<template>
    <Modal
            class-name="vertical-center-modal"
            v-model="modal"
            :width="60"
            :title="head_title">
        <div class="row" v-if="modal">
            <div class="col-lg-12">
                <div class="card ">
                    <Form ref="formValidate" :model="info" action="javascript:void(0)">
                        <div class="row">
                            <div class="col-md-12">
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">姓名</label>
                                        <div class="col-md-9">
                                            <Input v-model="info.name" placeholder="请输入姓名"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">手机号</label>
                                        <div class="col-md-9">
                                            <Input v-model="info.phone" placeholder="请输入手机号,并作为登录系统账户名"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem v-if="type!='edit'">
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">密码</label>
                                        <div class="col-md-9">
                                            <c-input v-model="info.password" autocomplete="new-password" type="password" placeholder="请输入密码"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem v-else>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">修改密码</label>
                                        <div class="col-md-9">
                                            <c-input v-model="info._edit_password" autocomplete="new-password" type="password" placeholder="请输入新密码，不填写则不更改"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">公司名称</label>
                                        <div class="col-md-9">
                                            <Input v-model="info.organization" placeholder="请输入公司名称"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">职位</label>
                                        <div class="col-md-9">
                                            <Input v-model="info.position" placeholder="请输入职位"/>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">角色</label>
                                        <div class="col-md-9">
                                            <status-select class='thumb-search' :p-data="info.role_id" p-name="qxgly_role" :p-far='true'  @on-change='searchSelectRole'></status-select>
                                        </div>
                                    </div>
                                </FormItem>
                                <FormItem>
                                    <div class="row">
                                        <label slot='label' class="control-label text-right col-md-3">状态</label>
                                        <div class="col-md-9">
                                            <status-select class='thumb-search' :p-data="info.status"  p-name='worker_status' :p-far="true"  @on-change='searchSelectStatus'></status-select>
                                        </div>
                                    </div>
                                </FormItem>
                            </div>
                        </div>
                    </Form>
                </div>
            </div>
        </div>
        <div slot="footer">
            <button class="btn btn-danger" type="button" @click='cancel'>取消</Button>
            <btn-submit class="btn btn-success" type="submit" :p-click="ok">确定</btn-submit>
        </div>
    </Modal>
</template>
<script>
    import serWorkerPerson from '../../serves/api-worker-person'
    const setDataDef = {
        name:"",
        phone:"",
        password:"",
        organization:"",
        position:"",
        role_id:"",
        status:"",
        _edit_password:"",
    };
    export default{
        props:{

        },
        data () {
            return {
                modal:false,
                type:'',
                info:{},
                file_book1:[],
                file_book2:[],
            }
        },
        computed:{
            head_title(){
                return this.type == 'edit' ? '编辑管理员':'添加管理员'
            },
        },
        methods: {
            async handleOpen(data){
                this.$set(this,'type',data._type||'add');
                this.info = Object.assign({},setDataDef,data);
                this.$set(this,'modal',true);
            },
            cancel () {
                this.$emit('on-change',false);
                this.$set(this,'modal',false);
            },
            searchSelectRole (value) {
                this.$set(this.info,'role_id',value);
            },
            searchSelectStatus (value) {
                this.$set(this.info,'status',value);
            },

            async ok () {
                let url = 'add';
                let data = {
                    name:this.info.name,
                    phone:this.info.phone,
                    password:this.info.password,
                    organization:this.info.organization,
                    position:this.info.position,
                    role_id:this.info.role_id,
                    status:this.info.status,
                };
                if(this.type=='edit'){
                    url = 'save';
                    data.id = this.info.id;
                    data.password = this.info._edit_password
                }
                let res = await serWorkerPerson[url](data);
                if(res.code==1){
                    this.$Message.success(res.message);
                    this.$emit('on-change',true);
                    this.$set(this,'modal',false);
                    return true;
                }else{
                    this.$Message.error(res.message);
                    return false;
                }
            },
        },
        components:{
        }
    }
</script>